package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.domain.SeckillOrder;
import cn.wolfcode.shop.mapper.SeckillGoodMapper;
import cn.wolfcode.shop.mapper.SeckillOrderMapper;
import cn.wolfcode.shop.redis.RedisServer;
import cn.wolfcode.shop.redis.SeckillOrderKeyPrefix;
import cn.wolfcode.shop.service.ISeckillOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private RedisServer redisServer;
    @Override
    public SeckillOrder getSeckillOrder(Long userId, Long goodId) {
        return seckillOrderMapper.selectSeckillOrder(userId,goodId);
    }
    @Transactional
    @Override
    public SeckillOrder createSeckillOrder(Long userId, Long goodId,String orderNo) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(userId);
        seckillOrder.setGoodId(goodId);
        seckillOrder.setOrderNo(orderNo);
        //插入秒杀订单
        seckillOrderMapper.insert(seckillOrder);
        //将秒杀订单缓存到数据库中
        redisServer.set(SeckillOrderKeyPrefix.SECKILL_ORDER,userId+":"+goodId,seckillOrder);
        return seckillOrder;
    }
}
